package com.hsck.order.kds.main.fragment.record.dialog

import android.app.Activity
import android.app.Dialog
import android.view.LayoutInflater
import android.view.WindowManager
import androidx.databinding.DataBindingUtil
import com.hsck.order.kds.R
import com.hsck.order.kds.databinding.DishSuccessDialogLayoutBinding
import kotlinx.coroutines.*

/**
 * 划菜成功
 */
class DishSuccessDialog(context: Activity) : Dialog(context, R.style.Dialog_FullScreen) {

    private var binding: DishSuccessDialogLayoutBinding = DataBindingUtil.inflate(
        LayoutInflater.from(context),
        R.layout.dish_success_dialog_layout,
        null,
        false
    )

    private val activity: Activity = context
    private var dismissJob: Job? = null

    init {
        createDialog()
    }

    @OptIn(DelicateCoroutinesApi::class)
    private fun createDialog() {
        setContentView(binding.root)
        setCanceledOnTouchOutside(true)
        setCancelable(true)
        val lp = window?.attributes
        lp?.width = WindowManager.LayoutParams.MATCH_PARENT
        lp?.height = WindowManager.LayoutParams.WRAP_CONTENT

        lp?.alpha = 1f
        window?.attributes = lp
        if (activity.isFinishing) {
            return
        }
        setOnDismissListener { dismissJob?.cancel() }
        setOnShowListener {
            dismissJob = GlobalScope.launch(Dispatchers.Main) {
                delay(2000)
                dismiss()
            }
        }
    }
}
